//
//  XLPopView.m
//  TipRect
//
//  Created by Jellyfish on 16/8/8.
//  Copyright © 2016年 Jellyfish. All rights reserved.
//

#import "XLPopView.h"
#import "UIView+Extension.h"


#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define PRESENTATION_ANIMATION_DURATION 0.5
#define DISMISS_ANIMATION_DURATION 0.5

@interface XLPopView ()

/** 弹出框里面承载容器的视图 */
@property (nonatomic, weak) UIImageView *containerView;

@end

@implementation XLPopView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
    }
    
    return self;
}

#pragma mark -- 第一种：右上角删除按钮，顶部固定"提示"title，中间提示文字，确定按钮
+ (XLPopView *)popViewWithTipStr:(NSString *)tipStr withTitleStr:(NSString *)titleStr {
    return [[self alloc] initWithTipStr:tipStr withTitleStr:titleStr];
}

- (XLPopView *)initWithTipStr:(NSString *)tipStr withTitleStr:(NSString *)titleStr {
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        // 弹出框后面的背景视图
        UIView *shawdowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 180)];
        shawdowView.center = self.center;
        shawdowView.backgroundColor  = [UIColor blackColor];
        shawdowView.alpha = 0.6;
//        [self addSubview:shawdowView];
        
        // 承载容器的视图
        UIImageView *containerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 270,180)];
        containerView.center = self.center;
        containerView.userInteractionEnabled = YES;
        containerView.layer.cornerRadius = 5.0f;
        containerView.clipsToBounds = YES;
        containerView.image = [UIImage imageNamed:@"alertBg"];
        [self addSubview:containerView];
        _containerView = containerView;
        
        CGFloat cHeight = containerView.height;
        // 顶部label
        UILabel *topL = [UILabel new];
        topL.text = titleStr;
        topL.textColor = [UIColor blackColor];
        topL.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:17.0];
        topL.textAlignment = NSTextAlignmentCenter;
        topL.frame = CGRectMake(_containerView.width/4, 20, _containerView.width/2, 30);
        [_containerView addSubview:topL];

        
        // topTitle与middleTitle 分割线
        UIView *topSeperateLine = [UIView new];
        topSeperateLine.frame = CGRectMake(0, cHeight/3+6, _containerView.width, 1);
        topSeperateLine.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.5];

        
        // 中间label
        UILabel *middleL = [UILabel new];
        middleL.text = tipStr;
        middleL.textColor = [UIColor blackColor];
        middleL.numberOfLines = 0;
        middleL.font = [UIFont systemFontOfSize:14.0];
        middleL.textAlignment = NSTextAlignmentCenter;

        middleL.lineBreakMode = NSLineBreakByTruncatingMiddle;
        middleL.frame = CGRectMake(20, CGRectGetMaxY(topSeperateLine.frame) - 0.5 *(_containerView.width * 0.2), _containerView.width * 0.85, cHeight*0.45);
        [_containerView addSubview:middleL];
        
        // 确认按钮
        UIButton *confirmBtn = [UIButton new];
        confirmBtn.frame = CGRectMake(_containerView.size.width * 0.25, CGRectGetMaxY(middleL.frame) +10, _containerView.frame.size.width * 0.5, 40);

        [confirmBtn setBackgroundImage:[UIImage imageNamed:@"OK"] forState:UIControlStateNormal];
        [_containerView addSubview:confirmBtn];
        [confirmBtn addTarget:self action:@selector(clickCloseBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}


// 点击右上角关闭按钮,弹出框消失
- (void)clickCloseBtn {
    [self dismiss];
}

// 点击确定按钮，执行代理方法,弹出框消失
- (void)clickConfirmBtn {
    if ([self.delegate respondsToSelector:@selector(popViewClickConfirmBtn)]) {
        [self.delegate popViewClickConfirmBtn];
    }
    [self dismiss];
}

- (void)showInView:(UIView *)view {
    
    _containerView.alpha = 0;
    [view addSubview:self];
    
    _containerView.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:PRESENTATION_ANIMATION_DURATION
                          delay:0
         usingSpringWithDamping:1.0
          initialSpringVelocity:1
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         _containerView.alpha = 1.0f;
                         _containerView.transform = CGAffineTransformIdentity;
                     } completion:^(BOOL finished) {
                         
                     }];
}

- (void)dismiss {
    [UIView animateWithDuration:DISMISS_ANIMATION_DURATION
                                  delay:0
                                options:UIViewAnimationOptionBeginFromCurrentState
                             animations:^{
                                 
                                 _containerView.alpha = 0.0;
                                 self.alpha = 0.0;
                                 
                             }
                             completion:^(BOOL finished) {
                                 
                                 [self removeFromSuperview];
                                 
                             }];
}

@end
